{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## conf.cfg \n",
    "\n",
    "port=8002"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tornado import web\n",
    "import tornado\n",
    "from tornado.options import define, options, parse_command_line\n",
    "\n",
    "# define 定义一些可以在命令行中传递的参数以及类型\n",
    "define('port', default=8008, help='run on the given port', type=int)\n",
    "define('debug', default=True, help='set tornado debug mode', type=bool)\n",
    "\n",
    "# 通过命令行解析参数\n",
    "# options.parse_command_line()\n",
    "# 通过配置文件解析参数\n",
    "options.parse_config_file('conf.cfg')\n",
    "# options 是一个类，全局只有一个options\n",
    "\n",
    "\n",
    "\n",
    "class MainHandler(web.RequestHandler):\n",
    "    async def get(self, *args, **kwargs):\n",
    "\n",
    "        self.write('hello world3')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app = web.Application([\n",
    "        ('/', MainHandler)\n",
    "    ], debug=options.debug, autoreload=True)\n",
    "    # options 变量使用\n",
    "    app.listen(options.port)\n",
    "    tornado.ioloop.IOLoop.current().start()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 通过启动命令指定端口\n",
    "\n",
    "python options_test.py --port=8002"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
